package com.ego.item.controller;

import com.ego.common.pojo.PageResult;
import com.ego.item.bo.SpuBo;
import com.ego.item.pojo.Sku;
import com.ego.item.pojo.SpuDetail;
import com.ego.item.service.GoodsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> page(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows)
    {
        return ResponseEntity.ok(goodsService.page(key,saleable,page,rows));
    }

    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> findById(@PathVariable("id") Integer id){
        SpuDetail detail = goodsService.querySpuDetailById(id);
        if (detail == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(detail);
    }

    @GetMapping("/skus")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("spuId") Long spuId) {
        List<Sku> skus = this.goodsService.querySkuBySpuId(spuId);
        if (skus == null || skus.size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(skus);
    }

    @GetMapping("/pageForES")
    public ResponseEntity<PageResult<SpuBo>> page(@RequestParam("pageNo") Integer pageNo, @RequestParam("size") Integer size){
        return ResponseEntity.ok(goodsService.pageForES(pageNo,size));
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/spubo/{id}")
    public ResponseEntity<SpuBo> queryGoodsById(@PathVariable("id") Long id){
        SpuBo spuBo=this.goodsService.queryGoodsById(id);
        if (spuBo == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(spuBo);
    }

    @GetMapping("/sku/{skuId}")
    public Sku querySkuById(@PathVariable("skuId") Long skuId){
        return goodsService.findSkuBySkuId(skuId);
    }

    @GetMapping("/sku")
    public List<Sku> querySkuListByIdList(@RequestParam("skuIdList") List<Long> skuIdList){
        return goodsService.findSkuListBySkuIdList(skuIdList);
    }

    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        goodsService.save(spuBo);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/reduceStock")
    public Boolean reduceStock(@RequestBody List<Map<Object, Object>> list){
        return goodsService.reduceStock(list);
    }

    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        goodsService.update(spuBo);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping()
    public ResponseEntity<Void> delete(@RequestParam Integer id){
        goodsService.delete(id);
        return ResponseEntity.ok().build();
    }
}
